package pe.com.certicom.scv.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import pe.com.certicom.scv.domain.*;
import pe.com.certicom.scv.service.PanderoService;
import pe.com.certicom.scv.validator.PanderoValidator;

@Controller
public class PanderoController {

	@Autowired
	private PanderoService PanderoService;

	@RequestMapping(value = "/pandero/consultar", method = RequestMethod.GET)
	public String processConsultarPandero(Model modelo,
		@ModelAttribute("Pandero") Pandero pandero, BindingResult result, SessionStatus status) {
		modelo.addAttribute("Pandero", new Pandero());
		status.setComplete();
		return "pandero/consultar";
	}
	
	@RequestMapping(value = "/pandero/buscar", method = RequestMethod.POST)
	public String processObtenerPandero(Model modelo, HttpSession session,
		@ModelAttribute("Pandero") Pandero pandero, BindingResult result,SessionStatus status) {
		//Chequeando si existe Tvperson
		Tvperson tvperson = PanderoService.existeTvperson(pandero.getCO_PERSONA());
		if(tvperson!=null){
			System.out.println(tvperson.getCO_PERSONA());
			System.out.println("Existe Tvperson");
			//Chequeando si existe Tvpanesp
			Tvpanesp tvpanesp = PanderoService.existeTvpanesp(pandero.getCO_PERSONA());
			if(tvpanesp!=null){
				System.out.println(tvpanesp.getCO_PERSONA());
				System.out.println("Existe Tvpanesp");
			}else{
				System.out.println("No existe Tvpanesp");
				PanderoService.insertTvpanesp(pandero.getCO_PERSONA());
				System.out.println("Tvpanesp insertado");
			}
			
			//Chequeando si existe Panesp2
			Panesp2 panesp2 = PanderoService.existePanesp2(pandero.getCO_PERSONA());
			if(panesp2!=null){
				System.out.println(panesp2.getCO_PERSONA());
				System.out.println("Existe Panesp2");
			}else{
				System.out.println("No existe Panesp2");
				PanderoService.insertPanesp2(pandero.getCO_PERSONA());
				System.out.println("Panesp2 insertado");
			}
			
			//Chequeando si existe Panespedv
			Panespedv panespedv = PanderoService.existePanespedv(pandero.getCO_PERSONA());
			if(panespedv!=null){
				System.out.println(panespedv.getCO_PERSONA());
				System.out.println("Existe Panespedv");
			}else{
				System.out.println("No existe Panespedv");
				PanderoService.insertPanespedv(pandero.getCO_PERSONA());
				System.out.println("Panespedv insertado");
			}
			
			//Chequeando si existe Panespveh
			Panespveh panespveh = PanderoService.existePanespveh(pandero.getCO_PERSONA());
			if(panespveh!=null){
				System.out.println(panespveh.getCO_PERSONA());
				System.out.println("Existe Panespveh");
			}else{
				System.out.println("No existe Panespveh");
				PanderoService.insertPanespveh(pandero.getCO_PERSONA());
				System.out.println("Panespveh insertado");
			}
			
		}else{System.out.println("No existe Tvperson");}
				
		Pandero panderodb = PanderoService.getPandero(pandero.getCO_PERSONA());
		String coper = pandero.getCO_PERSONA().toString();
				
		new PanderoValidator().validarPandero(pandero, panderodb, result);
		if (result.hasErrors())
			return "/pandero/consultar";
		else {
			modelo.addAttribute("Pandero", panderodb);
			modelo.addAttribute("panderomodificado", "N");
			modelo.addAttribute("coper", coper);
						
			status.setComplete();
			return "/pandero/modificar";
			
		}
	}
	
	@RequestMapping(value = "/pandero/buscar2", method = RequestMethod.GET)
	public String processObtenerPandero2(Model modelo, HttpSession session,
		@ModelAttribute("Pandero") Pandero pandero, BindingResult result,SessionStatus status) {
		//Chequeando si existe Tvperson
		Tvperson tvperson = PanderoService.existeTvperson(pandero.getCO_PERSONA());
		if(tvperson!=null){
			System.out.println(tvperson.getCO_PERSONA());
			System.out.println("Existe Tvperson");
			//Chequeando si existe Tvpanesp
			Tvpanesp tvpanesp = PanderoService.existeTvpanesp(pandero.getCO_PERSONA());
			if(tvpanesp!=null){
				System.out.println(tvpanesp.getCO_PERSONA());
				System.out.println("Existe Tvpanesp");
			}else{
				System.out.println("No existe Tvpanesp");
				PanderoService.insertTvpanesp(pandero.getCO_PERSONA());
				System.out.println("Tvpanesp insertado");
			}
			
			//Chequeando si existe Panesp2
			Panesp2 panesp2 = PanderoService.existePanesp2(pandero.getCO_PERSONA());
			if(panesp2!=null){
				System.out.println(panesp2.getCO_PERSONA());
				System.out.println("Existe Panesp2");
			}else{
				System.out.println("No existe Panesp2");
				PanderoService.insertPanesp2(pandero.getCO_PERSONA());
				System.out.println("Panesp2 insertado");
			}
			
			//Chequeando si existe Panespedv
			Panespedv panespedv = PanderoService.existePanespedv(pandero.getCO_PERSONA());
			if(panespedv!=null){
				System.out.println(panespedv.getCO_PERSONA());
				System.out.println("Existe Panespedv");
			}else{
				System.out.println("No existe Panespedv");
				PanderoService.insertPanespedv(pandero.getCO_PERSONA());
				System.out.println("Panespedv insertado");
			}
			
			//Chequeando si existe Panespveh
			Panespveh panespveh = PanderoService.existePanespveh(pandero.getCO_PERSONA());
			if(panespveh!=null){
				System.out.println(panespveh.getCO_PERSONA());
				System.out.println("Existe Panespveh");
			}else{
				System.out.println("No existe Panespveh");
				PanderoService.insertPanespveh(pandero.getCO_PERSONA());
				System.out.println("Panespveh insertado");
			}
			
		}else{System.out.println("No existe Tvperson");}
				
		Pandero panderodb = PanderoService.getPandero(pandero.getCO_PERSONA());
		String coper = pandero.getCO_PERSONA().toString();
		new PanderoValidator().validarPandero(pandero, panderodb, result);
		if (result.hasErrors())
			return "/pandero/consultar";
		else {
			modelo.addAttribute("Pandero", panderodb);
			modelo.addAttribute("panderomodificado", "N");
			modelo.addAttribute("coper", coper);
			status.setComplete();
			return "/pandero/modificar";
			
		}
	}
	
	@RequestMapping(value = "/pandero/modificar", method = RequestMethod.POST)
	public String processModificarPandero(Model modelo,
		@ModelAttribute("Pandero") Pandero pandero, BindingResult result, SessionStatus status) {
		new PanderoValidator().validarForm(pandero, result);
		if(!result.hasErrors()){
		modelo.addAttribute("panderomodificado", "S");
		PanderoService.modificarPandero(pandero);
		status.setComplete();
		return "pandero/consultar";
		}else{
		return "pandero/modificar";}
	}
	
	@RequestMapping(value = "/pandero/mostrarPend", method = RequestMethod.GET)
	public String processListarPanderoPendientes(Model modelo){
		List<Tvperson> list = new ArrayList<Tvperson>();
		list =PanderoService.obtenerTvperson();
		modelo.addAttribute("Tvperson", list);
		return "pandero/listarPend";
	}
	
	@RequestMapping(value = "/pandero/mostrarTerm", method = RequestMethod.GET)
	public String processListarPanderoFinalizadas(Model modelo){
		List<Tvperson> list = new ArrayList<Tvperson>();
		list =PanderoService.obtenerTvpersonTerm();
		modelo.addAttribute("Tvperson", list);
		return "pandero/listarTerm";
	}
	
	@RequestMapping(value = "/pandero/ubigeo", method = RequestMethod.GET)
	public String processListarPanderoUbigeoZona(Model modelo){
		List<Ubigeo> list = new ArrayList<Ubigeo>();
		list =PanderoService.obtenerUbigeo();
		modelo.addAttribute("Ubigeo", list);
		System.out.println(list.size());
		return "pandero/ubigeo";
	}

}

